﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telerik.Sitefinity;
using Telerik.Sitefinity.Blogs.Model;
using Telerik.Sitefinity.Events.Model;
using Telerik.Sitefinity.GenericContent.Model;
using Telerik.Sitefinity.Libraries.Model;
using Telerik.Sitefinity.News.Model;
using Telerik.Sitefinity.Services;

namespace Migration.Classes
{
	public static class MigrationMetaDataDefinitions
	{
		private static Dictionary<string, Type> m_ModulesMap;
		private static Dictionary<string, Type> m_TypesMap;
		private static Dictionary<string, List<string>> m_ModulesIgnoreFieldsMap;

		static MigrationMetaDataDefinitions()
		{
			m_ModulesMap = new Dictionary<string, Type>
			{
			    { "Telerik.Cms.Engine.Data.Providers.DefaultProvider", typeof(ContentItem) },
			    { "Telerik.News.Data.DefaultNewsProvider", typeof(NewsItem) },
			    { "Telerik.Blogs.Data.DefaultBlogProvider", typeof(BlogPost) },
			    { "Telerik.Events.Data.DefaultEventsProvider", typeof(Event) },
			    //{ "Telerik.Libraries.Data.DefaultProvider", typeof(Library) }
			};

			m_ModulesIgnoreFieldsMap = new Dictionary<string, List<string>>
			{
				{ "Telerik.Cms.Engine.Data.Providers.DefaultProvider", new List<string>() { "Author", "Category", "Description", "Name" } },
				{ "Telerik.News.Data.DefaultNewsProvider", new List<string>() { "Author", "Category", "Expiration Date", "Publication Date", "Source", "Summary", "Title" } },
				{ "Telerik.Blogs.Data.DefaultBlogProvider", new List<string>() { "Author", "Category", "Publication Date", "Title", "View Count", "BlogID" } },
				{ "Telerik.Events.Data.DefaultEventsProvider", new List<string>() { "Category", "City", "Contact Cell", "Contact Email", "Contact Name", "Contact Phone", "Contact Web", "Country", "Event End", "Event Start", "Expiration Date", "Geomapping Data", "Publication Date", "State", "Street", "Title" } },
				{ "Telerik.Libraries.Data.DefaultProvider", new List<string>() { "Author", "AlternateText", "Category", "Description", "Extension", "Height", "Name", "Size", "Width" } }
			};

			m_TypesMap = new Dictionary<string, Type>
			{
			    { "ShortText", typeof (String) },
			    { "LongText", typeof (String) },
			    { "DateTime", typeof (DateTime) },
			    { "Integer", typeof (Int32) },
			    { "FloatingPoint", typeof (Single) },
			    { "Boolean", typeof (Boolean) },
			    { "Guid", typeof (Guid) },
			    { "Binary", typeof (Object) }
			};
		}

		public static void ImportMetaDataFields(Telerik.Sitefinity.Services.Migration.MigrationProvider provider)
		{
			if (!m_ModulesMap.ContainsKey(provider.Type))
			{
				MigrationLogger.AppendLogMessage(String.Format("Skipping definitions for {0}", provider.Type));
				return;
			}

			bool restart = false;

			foreach (Telerik.Sitefinity.Services.Migration.MigrationMetaDataDefinition metaData in provider.MetaDataDefinitions)
			{
				if (!m_ModulesIgnoreFieldsMap[provider.Type].Contains(metaData.Caption))
				{
					bool res = false;
					try
					{
						App.WorkWith().DynamicData()
							.Type(m_ModulesMap[provider.Type])
							.Field()
							.TryCreateNew(metaData.Caption, m_TypesMap[metaData.ValueType], ref res);
					}
					catch (InvalidOperationException) { }

					restart = restart || res;
				}
			}

			if (restart)
			{
				App.WorkWith().DynamicData().Type(m_ModulesMap[provider.Type]).SaveChanges(true);
				SystemManager.RestartApplication(false);
				MigrationSettings.ApplicationRestarted = true;
			}

			MigrationLogger.AppendLogMessage(String.Format("Imported metadata field definitions for {0}", provider.Type));
		}
	}
}
